package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import pojo.Cargo;
import util.DBUtils;
import dao.CargoDao;

public class CargoImpl extends BaseDaoImpl<Cargo> implements CargoDao {

	@Override
	public int add(Cargo cargo) {
		String sql = "insert into cargos values(?,?,?,?,?,?)";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnection();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, UUID.randomUUID().toString());
			pst.setString(2, cargo.getCname());
			pst.setInt(3, cargo.getCnumber());
			pst.setDouble(4, cargo.getPrice());
			pst.setDate(5, new java.sql.Date(cargo.getAddTime().getTime()));
			pst.setString(6, cargo.getW_id());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	@Override
	public int addBatch(List<Cargo> cargos) {
		String sql = "insert into cargos values(?,?,?,?,?,?)";
		PreparedStatement pst = null;
		Connection conn = DBUtils.createConnection();
		try {
			conn.setAutoCommit(false);
			pst = conn.prepareStatement(sql);
			for (Cargo cargo : cargos) {
				pst.setString(1, UUID.randomUUID().toString());
				pst.setString(2, cargo.getCname());
				pst.setInt(3, cargo.getCnumber());
				pst.setDouble(4, cargo.getPrice());
				pst.setDate(5, new java.sql.Date(cargo.getAddTime().getTime()));
				pst.setString(6, cargo.getW_id());
				pst.addBatch();
			}
			pst.executeBatch();
			conn.commit();
		} catch (SQLException e) {
			logger.error(e);
			try {
				conn.rollback();
			} catch (SQLException e1) {
				logger.error(e);
			}
			return -1;
		}finally{
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				logger.error(e);
			}
			DBUtils.close(conn, pst, null);
		}
		return 1;
	}
	
	@Override
	public int delete(String id) {
		String sql = "delete cargos where id=?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		int m = 0;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, id);
			m = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	@Override
	public int update(Cargo cargo) {
		String sql = "update cargos set cname=?,cnumber=?,price=?,addTime=?,w_id=? where id=?";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnection();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, cargo.getCname());
			pst.setInt(2, cargo.getCnumber());
			pst.setDouble(3, cargo.getPrice());
			pst.setDate(4, new java.sql.Date(cargo.getAddTime().getTime()));
			pst.setString(5, cargo.getW_id());
			pst.setString(6, cargo.getId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	@Override
	public Cargo load(String id) {
		String sql = "select * from cargos where id=?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		Cargo cargo  = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, id);
			rs = pst.executeQuery();
			if(rs.next()) {
				cargo = new Cargo();
				cargo.setId(rs.getString("id"));
				cargo.setCname(rs.getString("cname"));
				cargo.setCnumber(rs.getInt("cnumber"));
				cargo.setPrice(rs.getDouble("price"));
				cargo.setAddTime(new Date(rs.getDate("addTime").getTime()));
				cargo.setW_id(rs.getString("w_id"));
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return cargo;	
	}

	@Override
	public List<Cargo> findAll() {
		String sql = "select * from cargos";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Cargo> cargos = new ArrayList<>(); 
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()) {
				Cargo cargo = new Cargo();
				cargo.setId(rs.getString("id"));
				cargo.setCname(rs.getString("cname"));
				cargo.setCnumber(rs.getInt("cnumber"));
				cargo.setPrice(rs.getDouble("price"));
				cargo.setAddTime(new Date(rs.getDate("addTime").getTime()));
				cargo.setW_id(rs.getString("w_id"));
				cargos.add(cargo);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return cargos;	
	}

	@Override
	public List<Cargo> findByWarehousre(String w_id) {
		String sql = "select * from cargos where w_id=?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Cargo> cargos = new ArrayList<>(); 
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, w_id);
			rs = pst.executeQuery();
			while(rs.next()) {
				Cargo cargo = new Cargo();
				cargo.setId(rs.getString("id"));
				cargo.setCname(rs.getString("cname"));
				cargo.setCnumber(rs.getInt("cnumber"));
				cargo.setPrice(rs.getDouble("price"));
				cargo.setAddTime(new Date(rs.getDate("addTime").getTime()));
				cargo.setW_id(rs.getString("w_id"));
				cargos.add(cargo);
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return cargos;	
	}

	
}
